// pages/profile/package/package.js
import request from "../../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务记录数据
    doctor: [],
    //服务包
    package: [],
    status: [
      { id: 1, name: "已完成" },
      { id: 2, name: "已取消" },
      { id: 3, name: "待审核" },
      { id: 4, name: "已驳回" },
      { id: 5, name: "待服务" }
    ],
    location: [
      { id: 1, name: "上门服务" },
      { id: 2, name: "机构门诊" }
    ]
  },

  //跳转到服务记录
  recode(event) {
    const id = event.currentTarget.dataset.item;
    console.log(id);
    wx.navigateTo({
      url: 'record/recode?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 获取用户的id
    let id = wx.getStorageSync("userid");
    let that = this;
    request({
      url: 'service_record/service_record/user/' + id,
      method: "GET",
      success: (res) => {
        const doctorData = res.data.data.map(record => {
          // 计算 statusName
          const statusItem = that.data.status.find(s => s.id === record.status);
          const statusName = statusItem ? statusItem.name : '未知状态';

          // 计算 locationName
          const locationItem = that.data.location.find(l => l.id === record.location);
          const locationName = locationItem ? locationItem.name : '未知方式';

          return {
            ...record,
            statusName, // 新增字段
            locationName // 新增字段
          };
        });
        that.setData({ doctor: doctorData });
        console.log('doctor', that.data.doctor);

        const requests = [];
        for (let a = 0; a < that.data.doctor.length; a++) {
          let packageid = that.data.doctor[a].packageId;
          requests.push(new Promise((resolve, reject) => {
            request({
              url: "package/package/" + packageid,
              method: "GET",
              success: (res) => {
                console.log('package', res);
                const currentPackage = that.data.package || [];
                const newPackage = currentPackage.concat(res.data.data);
                that.setData({
                  package: newPackage
                });
                resolve();
              },
              fail: (err) => {
                console.error('请求失败:', err);
                wx.showToast({
                  title: '数据获取失败，请检查网络',
                  icon: 'none'
                });
                reject(err);
              }
            });
          }));
        }

        Promise.all(requests).then(() => {
          // 所有请求完成后的操作
          console.log('所有请求已完成，数据已更新');
        }).catch((err) => {
          console.error('请求处理过程中出现错误:', err);
        });
      },
      fail: (err) => {
        console.error('获取服务记录失败:', err);
        wx.showToast({
          title: '获取服务记录失败，请检查网络',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})